<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const form = ref({
  username: '',
  password: '',
  avatar: '/mock-img.jpg' // 可选：上传或默认
})

const router = useRouter()

function handleRegister() {
  if (!form.value.username || !form.value.password) {
    alert('请填写完整信息')
    return
  }

  const users = JSON.parse(localStorage.getItem('users') || '[]')
  const exist = users.find(u => u.username === form.value.username)
  if (exist) {
    alert('用户名已被注册')
    return
  }

  users.push(form.value)
  localStorage.setItem('users', JSON.stringify(users))

  alert('注册成功，请登录')
  router.push('/login')
}
</script>


<template>
  <div class="auth-container">
    <el-card class="auth-card">
      <h2>注册</h2>
      <el-form :model="form" @submit.prevent="handleRegister" label-width="60px">
        <el-form-item label="账号" aria-placeholder="账号">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password" />
        </el-form-item>
        <el-form-item label="确认">
          <el-input type="password" v-model="form.confirmPassword" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister">注册</el-button>
          <div class="login-link"></div>
          <el-button link @click="$router.push('/login')">已有账号？去登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.auth-container {
  background: url('/bg.jpg') center/cover no-repeat;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.auth-card {
  background: rgba(255, 255, 255, 0.15); /* 半透明背景 */
  border-radius: 20px;
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(12px); /* 玻璃模糊效果 */
  -webkit-backdrop-filter: blur(12px); /* Safari 兼容 */
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 48px 36px;
  max-width: 400px;
  width: 90%;
  text-align: center;
  font-family: 'Segoe UI', sans-serif;
  transition: all 0.3s ease-in-out;
}

.auth-card h2 {
  color: #ffffff;
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 32px;
  letter-spacing: 1px;
}

/* Element Plus 表单样式覆盖 */
.auth-card .el-form {
  text-align: left;
  width: 80%;
}

.auth-card .el-form-item__label {
  color: rgba(255, 255, 255, 0.8);
  font-weight: 500;
}

.auth-card .el-input__inner {
  background-color: rgba(255, 255, 255, 0.2);
  border: none;
  color: #fff;
  border-radius: 10px;
  padding: 12px 16px;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.auth-card .el-input__inner::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.auth-card .el-input__inner:focus {
  background-color: rgba(255, 255, 255, 0.3);
}

.auth-card .el-button--primary {
  width: 100%;
  background: linear-gradient(to right, #f9d423, #ff4e50);
  border: none;
  border-radius: 30px;
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.auth-card .el-button--primary:hover {
  transform: scale(1.03);
  box-shadow: 0 4px 15px rgba(255, 78, 80, 0.4);
}

.auth-card .el-button--link {
  color: #ffe082;
  font-size: 14px;
  margin-top: 12px;
  text-decoration: underline;
}

.auth-card .el-button--link:hover {
  opacity: 0.8;
}
</style>